package com.zzusedating.androidapp.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zzusedating.androidapp.FollowFansListActivity;
import com.zzusedating.androidapp.ProfileActivity;
import com.zzusedating.androidapp.R;
import com.zzusedating.androidapp.SettingActivity;
import com.zzusedating.androidapp.customlayout.TextViewWithRightText;
import com.zzusedating.androidapp.customlayout.TitleBarLayout;
import com.zzusedating.androidapp.tools.MyApplication;

import org.json.JSONObject;

import java.util.Locale;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Callback;

public class MeFragment extends Fragment {
    private static final String TAG = "MeFragment";

    private CircleImageView headView;
    private TextView nameView;
    private TextView levelView;
    private TextView followView;
    private TextView followerView;
    private TextView popView;
    private TextView activeView;
    private TextView prizeView;
    private TextView selfDescView;
    private TextViewWithRightText activeDayView;
    private TextViewWithRightText maritalView;
    private TextViewWithRightText vMoneyView;
    private TitleBarLayout titleBar;

    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me_layout, container, false);

        titleBar = view.findViewById(R.id.Me_TitleBar);
        headView = view.findViewById(R.id.Me_Head);
        nameView = view.findViewById(R.id.Me_Name);
        levelView = view.findViewById(R.id.Me_Level);
        followView = view.findViewById(R.id.Me_Follow);
        followerView = view.findViewById(R.id.Me_Follower);
        popView = view.findViewById(R.id.Me_Pop);
        activeView = view.findViewById(R.id.Me_Active);
        prizeView = view.findViewById(R.id.Me_Prize);
        selfDescView = view.findViewById(R.id.Me_SelfDesc);
        activeDayView = view.findViewById(R.id.Me_ActiveDay);
        maritalView = view.findViewById(R.id.Me_Marital);
        vMoneyView = view.findViewById(R.id.Me_VirtualMoney);
        LinearLayout setting = view.findViewById(R.id.setting);
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), SettingActivity.class));
            }
        });

        titleBar.setClickListenerTo(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), ProfileActivity.class));
            }
        }, TitleBarLayout.TO_TEXT);
        headView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), ProfileActivity.class));
            }
        });
        followerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), FollowFansListActivity.class);
                intent.putExtra("which", FollowFansListActivity.fans);
                startActivity(intent);
            }
        });
        followView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), FollowFansListActivity.class);
                intent.putExtra("which", FollowFansListActivity.follows);
                startActivity(intent);
            }
        });

        init();

        return view;
    }

    private void init(){

        JSONObject jsonObject = MyApplication.getBasicProfile();
        if (jsonObject == null){
            Toast.makeText(MyApplication.getContext(), "获取用户信息失败", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            nameView.setText(jsonObject.getString("name"));
            levelView.setText(String.format(Locale.CHINA, "Lv.%d", jsonObject.getInt("vipLevel")));
            followView.setText(String.format(Locale.CHINA, "关注 %d", jsonObject.getInt("followNum")));
            followerView.setText(String.format(Locale.CHINA, "粉丝 %d", jsonObject.getInt("fansNum")));
            popView.setText(String.format(Locale.CHINA, "人气 %d", jsonObject.getInt("popularity")));
            activeView.setText(String.format(Locale.CHINA, "活跃 %d", jsonObject.getInt("activeValue")));
            prizeView.setText(String.format(Locale.CHINA, "赞 %d", jsonObject.getInt("praisedNum")));
            activeDayView.setRightText("" + jsonObject.getInt("activeDays"));
            selfDescView.setText(jsonObject.getString("selfIntro"));
            maritalView.setRightText(MyApplication
                    .getContext()
                    .getResources()
                    .getStringArray(R.array.marry_status_list)
                    [jsonObject.getInt("maritalStatus") - 1]);
            vMoneyView.setRightText(jsonObject.getInt("coins"));
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
